home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / smallt~1 / smallt~1.zoo / mst.y < prev    next >
Encoding:
Text File  |  1990-05-26  |  9.5 KB  |  420 lines

  1. /* -*- bison -*- */
  2.  
  3. /***********************************************************************
  4.  *
  5.  * Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  6.  * Written by Steve Byrne.
  7.  *
  8.  * This file is part of GNU Smalltalk.
  9.  *
  10.  * GNU Smalltalk is free software; you can redistribute it and/or modify it
  11.  * under the terms of the GNU General Public License as published by the Free
  12.  * Software Foundation; either version 1, or (at your option) any later 
  13.  * version.
  14.  * 
  15.  * GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  16.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  17.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  18.  * more details.
  19.  * 
  20.  * You should have received a copy of the GNU General Public License along with
  21.  * GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  22.  * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  23.  *
  24.  ***********************************************************************/
  25.  
  26. %{
  27. #include "mst.h"
  28. #include "mstsym.h"
  29. #include "msttree.h"
  30. #include "mstdict.h"
  31. #include <stdio.h>
  32. #ifdef HAS_ALLOCA_H
  33. #include <alloca.h>
  34. #endif
  35.  
  36. #define YYDEBUG 1
  37.  
  38. extern Boolean        quietExecution;
  39.  
  40. /*int yynerr;            /* hack around Bison bug for pure parser */
  41. /*int yydebug = 0;        /* hack around Bison bug for pure parser */
  42. %}
  43.  
  44. %pure_parser
  45.  
  46. %union{
  47.   char        cval;
  48.   double    fval;
  49.   long        ival;
  50.   char        *sval;
  51.   TreeNode    node;
  52. }
  53.  
  54. /* single definite characters */     
  55. %token BANG COLON UPARROW DOT ASSIGN SHARP SEMICOLON
  56. %token OPEN_PAREN CLOSE_PAREN OPEN_BRACKET CLOSE_BRACKET
  57. %token PRIMITIVE_START INTERNAL_TOKEN
  58.  
  59. /* larger lexical items */
  60. %token <sval> IDENTIFIER KEYWORD STRING_LITERAL SYMBOL_KEYWORD BINOP
  61.               VERTICAL_BAR 
  62. %token <ival> INTEGER_LITERAL
  63. %token <fval> FLOATING_LITERAL
  64. %token <cval> CHAR_LITERAL
  65.  
  66. %type <node> method message_pattern variable_name keyword_variable_list
  67.     temporaries variable_names statements non_empty_statements expression
  68.     assigns primary number symbol_constant symbol 
  69.     character_constant string array_constant array
  70.     array_constant_list block opt_block_variables 
  71.     block_variable_list unary_expression binary_expression
  72.     keyword_expression keyword_binary_object_description_list
  73.     cascaded_message_expression semi_message_list
  74.     message_elt simple_expression literal message_expression
  75.     array_constant_elt unary_object_description
  76.     binary_object_description
  77. %type <sval> unary_selector keyword binary_selector
  78. %type <ival> primitive
  79. %%
  80.  
  81. program:
  82.     class_definition_list
  83.     | internal_marker method    { compileMethod($2); }
  84.     ;
  85.  
  86. internal_marker:
  87.     INTERNAL_TOKEN            { clearMethodStartPos(); }
  88.  
  89. class_definition_list:
  90.     class_definition
  91.     | class_definition_list class_definition
  92.     ;
  93.  
  94. class_definition:
  95.     class_header method_list BANG
  96.     | class_header  BANG
  97.     | non_empty_statements BANG    { if (!hadError) {
  98.                         executeStatements(nil, $1,
  99.                                 quietExecution); 
  100.                       }
  101.                       hadError = false;
  102.                     }
  103.     | temporaries non_empty_statements BANG    
  104.                     { if (!hadError) {
  105.                         executeStatements($1, $2,
  106.                                 quietExecution); 
  107.                                           }
  108.                       hadError = false;
  109.                                         }
  110.     | error BANG            { hadError = false; }
  111.     ;
  112.  
  113. class_header:
  114.     BANG class_specification BANG     { clearMethodStartPos(); }
  115.     ;
  116.  
  117. class_specification:
  118.     simple_expression    { executeStatements(nil, 
  119.                     makeStatementList($1, nil), true); }
  120.     ;
  121.  
  122. /*
  123. method_list:
  124.     method                 { if (!hadError) {
  125.                         compileMethod($1);
  126.                       } else {
  127.                         hadError = false;
  128.                       }
  129.                     }
  130.         | method_list method        { if (!hadError) {
  131.                         compileMethod($2);
  132.                       } else {
  133.                         hadError = false;
  134.                       }
  135.                     }
  136.     ;
  137.      
  138. method:
  139.     message_pattern statements BANG 
  140.                     { $$ = makeMethod($1, nil, 0, $2); }
  141.     | message_pattern temporaries statements BANG
  142.                     { $$ = makeMethod($1, $2, 0, $3); }
  143.     | message_pattern primitive statements BANG
  144.                     { $$ = makeMethod($1, nil, $2, $3); }
  145.     | message_pattern temporaries primitive statements BANG
  146.         { $$ = makeMethod($1, $2, $3, $4); }
  147.     ;
  148.  
  149. */
  150. method_list:
  151.     method BANG             { if (!hadError) {
  152.                         compileMethod($1);
  153.                         clearMethodStartPos();
  154.                       } else {
  155.                         hadError = false;
  156.                       }
  157.                     }
  158.         | method_list method BANG    { if (!hadError) {
  159.                         compileMethod($2);
  160.                         clearMethodStartPos();
  161.                       } else {
  162.                         hadError = false;
  163.                       }
  164.                     }
  165.     ;
  166.      
  167. method:
  168.     message_pattern statements 
  169.                     { $$ = makeMethod($1, nil, 0, $2); }
  170.     | message_pattern temporaries statements
  171.                     { $$ = makeMethod($1, $2, 0, $3); }
  172.     | message_pattern primitive statements 
  173.                     { $$ = makeMethod($1, nil, $2, $3); }
  174.     | message_pattern temporaries primitive statements
  175.         { $$ = makeMethod($1, $2, $3, $4); }
  176.     ;
  177.  
  178.  
  179. message_pattern:
  180.     unary_selector            { $$ = makeUnaryExpr(nil, $1); }
  181.     | binary_selector variable_name    { $$ = makeBinaryExpr(nil, $1,
  182.                                       $2); }
  183.     | keyword_variable_list        { $$ = makeKeywordExpr(nil, $1); }
  184.     | error                { errorf("Invalid message pattern");
  185.                       hadError = true;
  186.                       $$ = nil; }
  187.     ;
  188.  
  189. unary_selector:
  190.     IDENTIFIER
  191.     ;
  192.  
  193. binary_selector:
  194.     BINOP            /* I don't like this usage of binop */
  195.     | VERTICAL_BAR
  196.     ;
  197.  
  198. variable_name:
  199.     IDENTIFIER            { $$ = makeVariable($1); }
  200.     ;
  201.  
  202. keyword_variable_list:
  203.     keyword variable_name        { $$ = makeKeywordList($1, $2); }
  204.     | keyword_variable_list keyword variable_name
  205.                     { addNode($1, makeKeywordList($2, $3));
  206.                       $$ = $1; }
  207.     ;
  208.  
  209. keyword:
  210.     KEYWORD
  211.     ;
  212.  
  213. primitive:
  214.     PRIMITIVE_START INTEGER_LITERAL BINOP
  215.                     { $$ = $2;
  216.                       if (strcmp($3, ">") != 0) {
  217.                         YYERROR;
  218.                       }
  219.                     }
  220.  
  221. temporaries:
  222.     VERTICAL_BAR VERTICAL_BAR    { $$ = nil; }
  223.     | VERTICAL_BAR variable_names VERTICAL_BAR
  224.                     { $$ = $2; }
  225.     ;
  226.  
  227. variable_names:
  228.     variable_name            { $$ = makeVariableList($1); }
  229.     | variable_names variable_name    { addNode($1, makeVariableList($2));
  230.                       $$ = $1; }
  231.     ;
  232.  
  233. statements:
  234.     /* empty */            { $$ = nil; }
  235.     | non_empty_statements
  236.     ;
  237.  
  238. non_empty_statements:
  239.     UPARROW expression     
  240.                 { $$ = makeStatementList(makeReturn($2),
  241.                                    nil); }
  242.     | expression        { $$ = makeStatementList($1, nil); }
  243.     | expression DOT statements
  244.                  /* I don't know if I like this production */
  245.                 { $$ = makeStatementList($1, $3); }
  246.     | error DOT statements  { $$ = $3;
  247.                   yyerrok;
  248.                   errorf("Error in expression");
  249.                   hadError = true;
  250.                 }
  251.     ;
  252.     
  253. expression:
  254.     simple_expression
  255.     | assigns simple_expression    { $$ = makeAssign($1, $2); }
  256.     ;
  257.  
  258. assigns:
  259.     variable_name ASSIGN        { $$ = makeVariableList($1); }
  260.     | assigns variable_name ASSIGN
  261.                     { addNode($1, makeVariableList($2));
  262.                       $$ = $1; }
  263.     ;
  264.  
  265. simple_expression:
  266.     primary
  267.     | message_expression
  268.     | cascaded_message_expression
  269.     ;
  270.  
  271. primary:
  272.     variable_name
  273.     | literal
  274.     | block                
  275.     | OPEN_PAREN expression CLOSE_PAREN { $$ = $2; }
  276.     ;
  277.  
  278. literal:
  279.     number
  280.     | symbol_constant
  281.     | character_constant
  282.     | string
  283.     | array_constant
  284.     ;
  285.  
  286. number:
  287.     INTEGER_LITERAL            { $$ = makeIntConstant($1); }
  288.     | FLOATING_LITERAL        { $$ = makeFloatConstant($1); }
  289.     ;
  290.  
  291. symbol_constant:
  292.     SHARP symbol            { $$ = makeSymbolConstant($2); }
  293.     ;
  294.  
  295. symbol:
  296.     IDENTIFIER            { $$ = internIdent($1); }
  297.     | binary_selector        { $$ = internBinOP($1); }
  298.     | SYMBOL_KEYWORD        { $$ = internIdent($1); }
  299.     | KEYWORD            { $$ = internIdent($1); }
  300.     ;
  301.  
  302.  
  303. character_constant:
  304.     CHAR_LITERAL            { $$ = makeCharConstant($1); }
  305.     ;
  306.  
  307. string:
  308.     STRING_LITERAL            { $$ = makeStringConstant($1); }
  309.     ;
  310.  
  311. array_constant:
  312.     SHARP array            { $$ = makeArrayConstant($2); }
  313.     ;
  314.  
  315. array:
  316.     OPEN_PAREN CLOSE_PAREN        { $$ = nil; }
  317.     | OPEN_PAREN array_constant_list CLOSE_PAREN
  318.                     { $$ = $2; }
  319.     ;
  320.  
  321. array_constant_list:
  322.     array_constant_elt        { $$ = makeArrayElt($1); }
  323.     | array_constant_list array_constant_elt
  324.                           { addNode($1, makeArrayElt($2));
  325.                       $$ = $1; }
  326.     ;
  327.  
  328. array_constant_elt:
  329.     number
  330.     | symbol
  331.     | string
  332.     | character_constant
  333.     | array
  334.     ;
  335.  
  336. block:
  337.     OPEN_BRACKET opt_block_variables statements CLOSE_BRACKET
  338.                     { $$ = makeBlock($2, $3); }
  339.     ;
  340.  
  341. opt_block_variables:
  342.     /* empty */            { $$ = nil; }
  343.     | block_variable_list VERTICAL_BAR
  344.     ;
  345.  
  346. /* syntax for blocks with temporaries is just args and vertical bar (if
  347.  * any followed by a standard temporaries declarations */
  348.  
  349. block_variable_list:
  350.     COLON variable_name        { $$ = makeVariableList($2); }
  351.     | block_variable_list COLON variable_name
  352.                         { addNode($1, makeVariableList($3));
  353.                       $$ = $1; }
  354.     ;
  355.  
  356. message_expression:
  357.     unary_expression
  358.     | binary_expression
  359.     | keyword_expression
  360.     ;
  361.  
  362. unary_expression:
  363.     unary_object_description unary_selector { $$ = makeUnaryExpr($1, $2); }
  364.     ;
  365.  
  366. unary_object_description:
  367.     primary
  368.     | unary_expression
  369.     ;
  370.  
  371. binary_expression:
  372.     binary_object_description binary_selector unary_object_description
  373.                     { $$ = makeBinaryExpr($1, $2, $3); }
  374.     ;
  375.  
  376. binary_object_description:
  377.     unary_object_description
  378.     | binary_expression
  379.     ;
  380.  
  381. keyword_expression:
  382.     binary_object_description keyword_binary_object_description_list
  383.                     { $$ = makeKeywordExpr($1, $2); }
  384.      ;
  385.  
  386. keyword_binary_object_description_list:
  387.     keyword binary_object_description
  388.                     { $$ = makeKeywordList($1, $2); }
  389.     | keyword_binary_object_description_list keyword
  390.       binary_object_description    { addNode($1, makeKeywordList($2, $3));
  391.                       $$ = $1; }
  392.     ;
  393.  
  394. cascaded_message_expression:
  395.     message_expression semi_message_list
  396.                        { $$ = makeCascadedMessage($1, $2); }
  397.     ;
  398.  
  399. semi_message_list:
  400.     SEMICOLON message_elt        { $$ = makeMessageList($2); }
  401.     | semi_message_list SEMICOLON message_elt
  402.                     { addNode($1, makeMessageList($3));
  403.                       $$ = $1; }
  404.     ;
  405.  
  406. message_elt:
  407.     unary_selector            { $$ = makeUnaryExpr(nil, $1); }
  408.     | binary_selector unary_object_description
  409.                     { $$ = makeBinaryExpr(nil, $1, $2); }
  410.     | keyword_binary_object_description_list
  411.                     { $$ = makeKeywordExpr(nil, $1); }
  412.     ;
  413.  
  414.  
  415. %%
  416. /*     
  417. ADDITIONAL C CODE
  418. */
  419.  
  420.